import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: localStorage.getItem('token') ? localStorage.getItem('token') : null,
    userInfo: localStorage.getItem('userInfo')?localStorage.getItem('userInf o'):null
  },
  mutations: {
    setToken: (state, token) => {
      state.token = token
    },
    setUserInfo: (state, userInfo) => {
      state.userInfo = userInfo

    },
  },
  actions: {
    login(context, userInfo) {
      console.log(userInfo)
      const {
        nickName,
        avatarUrl
      } = userInfo
      context.commit('setToken', userInfo.token)
      context.commit('setUserInfo', JSON.stringify(userInfo))
    },
    logout(context) {
      context.commit('setToken', null)
      context.commit('setUserInfo', null)
    }
  },
  modules: {}
})